<template>
    <div>
        <el-form :inline="true" class="demo-form-inline" >
            <el-form-item>
                <!--新增按钮-->
                <el-button type="primary" @click="dialogFormVisible = true">新增</el-button>
            </el-form-item>
        </el-form>
        <el-table
                :data="menuList"
                style="width: 100%;margin-bottom: 20px;"
                row-key="id"
                border
                stripe
                max-height="630"
                default-expand-all
                :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
            <el-table-column
                    prop="name"
                    label="名称"
                    sortable
                    width="180">
            </el-table-column>
            <el-table-column
                    prop="perms"
                    label="权限编码"
                    sortable
                    width="180">
            </el-table-column>
            <el-table-column
                    prop="icon"
                    label="图标">
            </el-table-column>
            <el-table-column
                    prop="type"
                    label="类型">
                <template slot-scope="scope">
                    <el-tag size="small" v-if="scope.row.type === 0">目录</el-tag>
                    <el-tag type="warning" size="small" v-else-if="scope.row.type === 1">菜单</el-tag>
                    <el-tag type="info" size="small" v-else-if="scope.row.type === 2">按钮</el-tag>
                </template>
            </el-table-column>
            <el-table-column
                    prop="path"
                    label="菜单URL">
            </el-table-column>
            <el-table-column
                    prop="component"
                    label="菜单组件">
            </el-table-column>
            <el-table-column
                    prop="orderNum"
                    sortable
                    label="排序号">
            </el-table-column>
            <el-table-column
                    prop="status"
                    label="状态">
                <template slot-scope="scope">
                    <el-tag v-if="scope.row.status" type="success" size="small">正常</el-tag>
                    <el-tag v-else type="danger" size="small">禁用</el-tag>
                </template>
            </el-table-column>
            <el-table-column
                    prop="options"
                    label="操作">
                <template slot-scope="scope">
                    <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
                    <el-divider direction="vertical"></el-divider>
                    <el-popconfirm
                            title="确定删除这一项内容吗？"
                            @confirm="handleDelete(scope.row.id)"
                    >
                        <el-button  size="mini" type="danger" slot="reference">删除</el-button>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>
        <!--操作对话框-->
        <el-dialog
                width="600px"
                title="新增菜单"
                :visible.sync="dialogFormVisible"
                :before-close="handleClose"
        >
            <el-form :model="editForm" :rules="editFormRules" ref="editForm" label-width="100px" class="demo-editForm">
                <el-form-item label="上级菜单" prop="parentId">
                    <el-select v-model="editForm.parentId" placeholder="请选择上级菜单">
                        <template v-for="item in menuList">
                            <el-option :label="item.name" :value="item.id"></el-option>
                            <template v-for="child in item.children">
                                <el-option :label="child.name" :value="child.id">
                                    <span>{{' - '+child.name}}</span>
                                </el-option>
                            </template>
                        </template>
                    </el-select>
                </el-form-item>
                <el-form-item label="菜单名称" prop="name">
                    <el-input v-model="editForm.name"></el-input>
                </el-form-item>
                <el-form-item label="权限编码" prop="perms">
                    <el-input v-model="editForm.perms"></el-input>
                </el-form-item>
                <el-form-item label="图标" prop="icon">
                    <el-input v-model="editForm.icon"></el-input>
                </el-form-item>
                <el-form-item label="菜单URL" prop="path">
                    <el-input v-model="editForm.path"></el-input>
                </el-form-item>
                <el-form-item label="菜单组件" prop="component">
                    <el-input v-model="editForm.component"></el-input>
                </el-form-item>
                <el-form-item label="类型" prop="type">
                    <el-radio-group v-model="editForm.type">
                        <el-radio :label=0>目录</el-radio>
                        <el-radio :label=1>菜单</el-radio>
                        <el-radio :label=2>按钮</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="状态" prop="status">
                    <el-radio-group v-model="editForm.status">
                        <el-radio :label=1>正常</el-radio>
                        <el-radio :label=0>禁用</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="排序号" prop="orderNum">
                    <el-input-number v-model="editForm.orderNum" :min="1"></el-input-number>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="resetForm('editForm')">取 消</el-button>
                <el-button type="primary" @click="submitForm('editForm')">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
  export default {
    name: 'Tabs',
      data() {
          return {
              menuList: [],
              dialogFormVisible: false,
              editForm: {
                  parentId: '',
                  name: '',
                  perms: '',
                  icon: '',
                  type: '',
                  status: '',
                  orderNum:''
              },
              editFormRules: {
                  parentId: [
                      { required: true, message: '请选择上级菜单', trigger: 'blur' },
                  ],
                  name: [
                      { required: true, message: '请输入菜单名称', trigger: 'blur' }
                  ],
                  perms: [
                      { required: true, message: '请输入权限编码', trigger: 'blur' }
                  ],
                  icon: [
                      { required: true, message: '请输入图标', trigger: 'blur' }
                  ],
                  type: [
                      { required: true, message: '请选择菜单类型', trigger: 'blur' }
                  ],
                  status: [
                      { required: true, message: '请选择菜单状态', trigger: 'blur' }
                  ],
                  orderNum: [
                      { required: true, message: '请输入排序号', trigger: 'blur' }
                  ]
              }
          }
      },
      created() {
          this.getMenuList();
      },
      methods: {
        // 获取菜单列表
          getMenuList() {
              this.$axios.get('/sys/menu/list').then(res => {
                  this.menuList = res.data.data;
              });
          },
          // 添加菜单
          submitForm(formName) {
              this.$refs[formName].validate(valid => {
                  if (valid) {
                      this.$axios.post('/sys/menu/'+(this.editForm.id?'update':'save'), this.editForm).then(res => {
                          this.$message({
                              showClose: true,
                              message: '操作成功~',
                              duration: 1000,
                              type: 'success',
                              onClose: () => {
                                  this.getMenuList();
                              }
                          });
                          this.dialogFormVisible = false;
                          this.resetForm(formName);
                      });
                  } else {
                      this.$message.error('操作失败！');
                      return false;
                  }
              })
          },
          handleEdit(item) {
              /*this.$axios.get('/sys/menu/info/'+i).then(res => {
                  this.editForm = res.data.data;
                  this.dialogFormVisible = true;
              });*/
              this.editForm = JSON.parse(JSON.stringify(item));
              this.dialogFormVisible = true;
          },
          resetForm(formName) {
              this.$refs[formName].resetFields();
              this.editForm = {
                  parentId: '',
                  name: '',
                  perms: '',
                  icon: '',
                  type: '',
                  status: '',
                  orderNum:''
              };
              this.dialogFormVisible = false;
          },
          handleClose() {
              this.resetForm('editForm');
          },
          handleDelete(id) {
              this.$axios.post('/sys/menu/delete/' + id).then(res => {
                  this.$message({
                      showClose: true,
                      message: '操作成功~',
                      duration: 1000,
                      type: 'success',
                      onClose: () => {
                          this.getMenuList();
                      }
                  });
              });
          },
      }
  }
</script>
